/**
 * Created by jade on 2014-12-01.
 */

var G_ScreenHeight = parseInt($(window).height(),10) - 140;
var G_subMenu = new Array();
var G_subMenuLink = new Array();
var G_subMenuMargin = new Array();
var G_w = "";
var G_h = "";

var fnGetSubMenus = function() {
    G_subMenu[0] = [];
    G_subMenu[1] = ['Express', 'Express 요금'];
    G_subMenu[2] = ['항공(Inbound)', '항공(Outbound)'];
    G_subMenu[3] = ['해상(Inbound)', '해상(Outbound)'];
    G_subMenu[4] = ['거래처'];
    G_subMenu[5] = ['매출관리', '매입관리', '청구서', '세금계산서 ', '통계'];
    G_subMenu[6] = ['국가코드 ', '지역코드 ', '공항코드 ', '항구코드 ', '공통코드 '];

    G_subMenuLink[0] = [];
    G_subMenuLink[1] = ['express.html', 'rateExpress.html'];
    G_subMenuLink[2] = ['airWayBillIn.html', 'airWayBillOut.html'];
    G_subMenuLink[3] = ['seaWayBillIn.html', 'seaWayBillOut.html'];
    G_subMenuLink[4] = ['company.html'];
    G_subMenuLink[5] = ['income.html', 'payment.html', 'invoice.html', 'esero.html', 'stat.html'];
    G_subMenuLink[6] = ['nation.html', 'zone.html', 'airport.html', 'port.html', 'comCode.html'];

    G_subMenuMargin[0] = 0;
    G_subMenuMargin[1] = 103;
    G_subMenuMargin[2] = 210;
    G_subMenuMargin[3] = 318;
    G_subMenuMargin[4] = 424;
    G_subMenuMargin[5] = 300;
    G_subMenuMargin[6] = 274;


    $("img.ui-datepicker-trigger").attr("style","margin-left:3px; vertical-align:middle; cursor:pointer;");
    $("#ui-datepicker-div").hide();

}

var G_calendar = {
    monthNamesShort: ['1월','2월','3월','4월','5월','6월','7월','8월','9월','10월','11월','12월'],
    dayNamesMin: ['일','월','화','수','목','금','토'],
    weekHeader: 'Wk',
    dateFormat: 'yy-mm-dd',														// 날짜형식(2012-03-03)
    showOn:"button",
    buttonImage:"/img/calendar.png",
    buttonImageOnly:true,
    autoSize: false,																	//자동리사이즈(false 이면 상위 정의에 따름)
    changeMonth: true,															// true이면 월변경, false이면 월변경못함
    changeYear: true,																// true이면 년변경, false이면 년변경못함
    showMonthAfterYear: true,													//년 뒤에 월 표시
    buttonImageOnly: true,														//이미지표시
    yearRange: 'c-99:c+99',													//1990년부터 2020년까지
    maxDate: '+2Y',																	// 오늘부터6년후날짜까지만, '+0d' 오늘 이전 날짜만 선택
    minDate: '-5y'
};

var fnGetLeftMenus = function(cd) {
    var leftMenu = '';
    for (c=0; c<G_subMenu[cd].length; c++)
    {
        leftMenu += '<div onClick="fnGoPage(\''+G_subMenuLink[cd][c]+'\')"><img src="/img/folder.jpg"> '+G_subMenu[cd][c]+'</div>';
    }
    $("#_curSubMenu").html(leftMenu);
}

var fnShowSubMenu = function(cd) {
    var subMenu = '';
    var leftMenu = '';
    for (c=0; c<G_subMenu[cd].length; c++)
    {
        if (c == 0) subMenu = '<div  style="float:left;margin-right:10px;margin-top:2px;margin-left:'+G_subMenuMargin[cd]+'px;">| </div>';
        subMenu += '<div onclick="fnGoPage(\''+G_subMenuLink[cd][c]+'\')" style="float:left;margin-right:5px;cursor:pointer;margin-top:2px;"> '+G_subMenu[cd][c]+'</div><div onclick="fnMainOpen(\''+G_subMenuLink[cd][c]+'\',\''+G_subMenuLink[cd][c]+'\');" style="float:left;margin-right:10px;margin-top:2px;cursor:pointer;"> <img src="/img/newWin.png" style="vertical-align:middle"> </div><div style="float:left;margin-right:10px;margin-top:2px;"> | </div>';
    }
    $("#_subMenu").html(subMenu);
    $("#_subMenu").show(100);
    //alert(subMenu);
}

var fnStaySubMenu = function() {
    $("#_subMenu").show();
}

var fnHideSubMenu = function() {
    $("#_subMenu").hide();
}

var fnSetCurMenu = function(cd) {
    $('#_mainMenu div:nth-child('+cd+')').css("color","#186096");
    $('#_mainMenu div:nth-child('+cd+')').css("background-color","#FFF");
    $('#_mainMenu div:nth-child('+cd+')').css("height","31px");
    $('#_mainMenu div:nth-child('+cd+')').css("margin-top","2px");
}

var fnMainOpen = function(target, winName) {
    window.open(target, winName, "width=1024,height=950,left:200,resizable=0, scrollbars=1")
}

//## 페이지 이동
var fnGoPage = function(target) {
    location.href = target;
}

//## 날짜 자동세팅하기
var fnSetDate = function(obj) {
    var str10 = obj.value.split(' ').join('');
    var strDt = "";
    if (str10.length == 8 && str10.indexOf('-') < 0)
    {
        strDt = str10.substr(0,4) + "-" + str10.substr(4,2) + "-" + str10.substr(6,2);
        obj.value = strDt;
    }
}

//## 천단위 코마(,) 찍어 해당 폼에 표기하기
var fnCommify = function(obj)
{
    n = obj.value
    var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
    n += '';                          // 숫자를 문자열로 변환

    var num = "";
    for (k=0; k<=n.length; k++)
    {
        if (n.substr(k,1) != "," && n.substr(k,1) != "") num += n.substr(k,1);
    }

    while (reg.test(num))
        num = num.replace(reg, '$1' + ',' + '$2');

    obj.value = num;
}

//## 천단위 코마(,) 찍어 반환하기
var fnGetNumWithComma = function(n)
{
    var reg = /(^[+-]?\d+)(\d{3})/;   // 정규식
    n += '';                          // 숫자를 문자열로 변환

    var num = "";
    for (k=0; k<=n.length; k++)
    {
        if (n.substr(k,1) != "," && n.substr(k,1) != "") num += n.substr(k,1);
    }

    while (reg.test(num))
        num = num.replace(reg, '$1' + ',' + '$2');

    return num;
}

var fnSetTax = function() {
    var amount = $("#amount").val();
    amount = amount.split(",").join("");
    if (amount > 0)
    {
        var tax = 0;
        tax = Math.round(parseInt(amount,10)/11);

        var orgAmount = parseInt(amount,10) - parseInt(tax,10);
        orgAmount = fnGetNumWithComma(orgAmount);
        tax = fnGetNumWithComma(tax);

        $("#tax").val(tax);
        $("#orgAmount").val(orgAmount);
    }
}

var fnRecount = function() {
    var amount = $("#amount").val().split(",").join("");
    var tax = $("#tax").val().split(",").join("");

    var orgAmount = parseInt(amount,10) - parseInt(tax,10);
    orgAmount = fnGetNumWithComma(orgAmount);

    $("#orgAmount").val(orgAmount);
}